package com.hkbigdata.sybx.mapper;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.hkbigdata.sybx.bean.ElderHealthData;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author yangjing
 * @creat 2025-05-28-10:01
 * @email 3010335359@qq.com
 */
@DS("mysql")
@Mapper
public interface ElderHealthDataMapper {
    // 修改后的表创建SQL（确保外键引用的表存在）
    @Update({
            "CREATE TABLE IF NOT EXISTS elder_health_data (",
            "  `id` INT AUTO_INCREMENT PRIMARY KEY COMMENT '老人健康数据值id',",
//            "  `pressure` INT NOT NULL COMMENT '血压',",
            "  `systolic_pressure` INT NOT NULL COMMENT '收缩压',",
            "  `diastolic_pressure` INT NOT NULL COMMENT '舒张压',",
            "  `blood_sugar` DOUBLE NOT NULL COMMENT '血糖',",
            "  `blood_lipid` DOUBLE NOT NULL COMMENT '血脂',",
            "  `heart_rate` INT NOT NULL COMMENT '心率',",
            "  `record_time` DATETIME NOT NULL COMMENT '时间',",
            "  `user_id` VARCHAR(20) NOT NULL COMMENT '关联的用户ID',",
            "  FOREIGN KEY (`user_id`) REFERENCES user_regedit(`user_id`) ON DELETE CASCADE ON UPDATE CASCADE,",
            "  INDEX idx_user_id (user_id)",
            ")",
            "ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci"
    })
    void createTable();

//    @Insert({
//            "INSERT INTO elder_health_data (pressure, blood_sugar, blood_lipid, heart_rate, record_time, user_id) ",
//            "VALUES (#{data.pressure}, #{data.bloodSugar}, #{data.bloodLipid}, #{data.heartRate}, #{data.recordTime}, #{data.userId})"
//    })
    @Insert({
            "INSERT INTO elder_health_data (systolic_pressure, diastolic_pressure, blood_sugar, blood_lipid, heart_rate, record_time, user_id) ",
            "VALUES (#{data.systolicPressure}, #{data.diastolicPressure}, #{data.bloodSugar}, #{data.bloodLipid}, #{data.heartRate}, #{data.recordTime}, #{data.userId})"
    })
    void insertData(@Param("data") ElderHealthData data);

    @Select("SELECT systolic_pressure as systolicPressure, diastolic_pressure as diastolicPressure, blood_sugar as bloodSugar," +
            " blood_lipid as bloodLipid, heart_rate as heartRate," +
            " record_time as recordTime, user_id as userId FROM elder_health_data WHERE user_id = #{userId}")
    List<ElderHealthData> selectByUserId(String userId);
}